package com.gomyb.oss.config;

import com.gomyb.oss.FileService;
import com.gomyb.oss.LocalStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/7/3 23:13
 */
@Configuration
@EnableConfigurationProperties(ConfigProperties.class)
@Slf4j
public class StorageConfig {

    @Autowired
    private ConfigProperties configProperties;

    @Bean
    @ConditionalOnProperty(name = {"file.source"}, havingValue = "com.gomyb.oss.AliOSS", matchIfMissing = true)
    public FileService fileUpload() {
        try {
            FileService handle = (FileService) Class.forName(configProperties.getSource().getName()).newInstance();
            handle.setConfigProperties(configProperties);
            return handle;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("存储规则设定失败，启用本地存储");
        }
        return new LocalStorage();
    }


}
